package com.production.CoursePlatform.service;

import com.production.CoursePlatform.entity.User;
import com.production.CoursePlatform.mapper.UserMapper;
import com.production.CoursePlatform.response.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;
    public int login(String IDnumber, String password){

        User user = userMapper.login(IDnumber);

        // 校验密码
        if (user != null && password.equals(user.getPassword())) {
            return user.getIsTeacher();

        } else {
            return 3;
        }
    }


    @Transactional
    public ApiResponse changePassword(String IDnumber, String oldPassword, String newPassword) {
        ApiResponse response = new ApiResponse();
        User user = userMapper.login(IDnumber);

        // 校验密码
        if (user != null && oldPassword.equals(user.getPassword())) {
            // 修改密码
            userMapper.update(IDnumber, newPassword);
            response.setMessage("Password has been changed");
            response.setSuccess(true);
        } else {
            response.setMessage("Wrong old password or user not found");
            response.setSuccess(false);
        }
        return response;
    }
}

